package com.baosight.ice;

import com.baosight.xcyber.DemoPrx;
import com.zeroc.Ice.*;
import com.zeroc.Ice.Object;
import com.zeroc.Ice.Process;

import java.text.MessageFormat;
import java.util.Map;

public class IceCaller {
    public static void main(String args[]) {
        System.out.println(Runtime.getRuntime().availableProcessors());
        Communicator communicator = Util.initialize(new String[]{
                "--Ice.Default.Locator=IceGrid/Locator:tcp -h 10.25.25.129 -p 12000",
                "--Ice.Admin.Enabled=1"});
        communicator.stringToProxy("xCyber/TreeManager2");


        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        ObjectPrx objectPrx = communicator.stringToProxy("xCyber/Demo");
        DemoPrx demoPrx = DemoPrx.checkedCast(objectPrx);
        float v = demoPrx.callMiddle(991);
        System.out.println(MessageFormat.format("this result is {0}", v));
        ((Process)communicator.findAdminFacet("Process")).shutdown(null);
        System.out.println("end");

        System.out.println("start invoke");
        OutputStream outputStream=new OutputStream();
        outputStream.startEncapsulation();
        outputStream.writeInt(112);
        outputStream.endEncapsulation();
        Object.Ice_invokeResult callMiddle = objectPrx.ice_invoke("callMiddle", OperationMode.Normal, outputStream.finished());
        if(callMiddle.returnValue){
            InputStream inputStream = new InputStream(callMiddle.outParams);
            System.out.println(MessageFormat.format("invoke result is {0}",inputStream.readFloat()));
        }
        System.out.println("end invoke");


        LoggerAdminPrx loggerAdminPrx = LoggerAdminPrx.checkedCast(demoPrx, "Logger");
        PropertiesAdminPrx propertiesAdminPrx = PropertiesAdminPrx.checkedCast(demoPrx, "Properties");
        ProcessPrx processPrx = ProcessPrx.checkedCast(demoPrx, "Process");

        Map<String, String> propertiesForPrefix = propertiesAdminPrx.getPropertiesForPrefix("");
        for (Map.Entry<String, String> entry : propertiesForPrefix.entrySet()) {
            System.out.println(MessageFormat.format("{0}:{1}", entry.getKey(), entry.getValue()));
        }
        if (processPrx != null) {
            System.out.println("i go 1 way");
        } else {
            System.out.println("i go 2 way");
            ObjectPrx admin = communicator.getAdmin();

            processPrx = ProcessPrx.checkedCast(admin, "Process");
        }
        processPrx.writeMessage("mes123", 1);
        processPrx.shutdown();
    }
}
